#include "ScnsJoystick.c.h"

#if defined(SCNS_JOYSTICK_ENABLE)&&SCNS_JOYSTICK_ENABLE==1

#include "ScnsYing.h"
#include "ScnsMenu.h"
#include "ScnsSmctpExtOscilloscope.h"

#if __SCNS_DEVICE_ID__==1
static const ADCCH_enum ch[SCNS_JOYSTICK_MAX][2]={
    {ADC1_CH00_A00,ADC1_CH01_A01},
    {ADC1_CH04_A04,ADC1_CH05_A05},
};

const ScnsBspJoystickConfig scnsBspJoystickConfig[SCNS_JOYSTICK_MAX]={
    {
        .key={.L=SCNS_KEY_LJL,.R=SCNS_KEY_LJR,.U=SCNS_KEY_LJU,.D=SCNS_KEY_LJD},
        .correct={.xIsCh=0,.ER=42,.ceX=1982,.ceY=2098,.k={-17.081453,-16.763479,16.986456,16.790836},},
    },
    {
        .key={.L=SCNS_KEY_RJL,.R=SCNS_KEY_RJR,.U=SCNS_KEY_RJU,.D=SCNS_KEY_RJD},
        .correct={.xIsCh=1,.ER=42,.ceX=2064,.ceY=2132,.k={-16.512844,-17.437513,16.857182,17.145245},},
    },
};
#elif __SCNS_DEVICE_ID__==2
static const ADCCH_enum ch[SCNS_JOYSTICK_MAX][2]={
    {ADC1_CH00_A00,ADC1_CH01_A01},
    {ADC1_CH02_A02,ADC1_CH03_A03},
};

const ScnsBspJoystickConfig scnsBspJoystickConfig[SCNS_JOYSTICK_MAX]={
    {
        .key={.L=SCNS_KEY_LJL,.R=SCNS_KEY_LJR,.U=SCNS_KEY_LJU,.D=SCNS_KEY_LJD},
.correct={.xIsCh=0,.ER=18,.ceX=2170,.ceY=1994,.k={-15.248494,-17.182486,16.593882,15.730677},},



    },
    {
        .key={.L=SCNS_KEY_RJL,.R=SCNS_KEY_RJR,.U=SCNS_KEY_RJU,.D=SCNS_KEY_RJD},
       .correct={.xIsCh=0,.ER=576,.ceX=1821,.ceY=2646,.k={-26.372704,-19.297409,15.857570,37.533791},},

     
   },
};
#else
#error
#endif

ScnsStatus scnsBspJoystickInit(ScnsJoystickEnum joystickN)
{
    adc_init(ADC_1,ch[joystickN][0],ADC_12BIT);
    adc_init(ADC_1,ch[joystickN][1],ADC_12BIT);
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspJoystickPostInit(ScnsJoystickEnum joystickN)
{
    scnsMenuSetPage(1,"joystick",NULL);
    if(joystickN==SCNS_JOYSTICK_L)
    {
        scnsMenuAddConstStruct(1,scnsJoystickResult[joystickN],x,"L",SCNS_TYPE_INT16);
        scnsMenuAddConstStruct(1,scnsJoystickResult[joystickN],y,"L",SCNS_TYPE_INT16);
    }
    else if(joystickN==SCNS_JOYSTICK_R)
    {
        scnsMenuAddConstStruct(1,scnsJoystickResult[joystickN],x,"R",SCNS_TYPE_INT16);
        scnsMenuAddConstStruct(1,scnsJoystickResult[joystickN],y,"R",SCNS_TYPE_INT16);
    }
    return SCNS_STATUS_OK;
}

void scnsBspJoystickPrepareAllData(void)
{
}

ScnsJoystickSource scnsBspJoystickGet(ScnsJoystickEnum joystickN)
{
    return (ScnsJoystickSource){
        .ch={
            adc_convert(ADC_1,ch[joystickN][0]),
            adc_convert(ADC_1,ch[joystickN][1]),
        }
    };
}

void scnsBspJoystickUpdateResult(ScnsJoystickEnum joystickN)
{

}

#endif
